package com.adee.nowcoder.huawei;

import java.util.Scanner;

public class T023_HJ24_0 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int[] arr = new int[n];
            for(int i = 0; i < n; i++) {
                arr[i] = in.nextInt();
            }
            int[] max = maxChildSerial(arr,0,n-1);
            for(int i = 0; i < max.length; i++) {
                System.out.print(max[i] + " ");
            }
        }
    }
    // 最长递增子序列长度
    public static int[] maxChildSerial(int[] arr, int a, int b) {
        int[][] dp = new int[b-a+1][];
        for(int i = 0; i <b-a+1; i++){
            dp[i] = new int[]{arr[i+a]};
        }
        int[] max = new int[0];
        for(int i = 1; i <b-a+1; i++){
            for(int j = 0; j < i; j++) {
                if(arr[i+a] > arr[j+a]) {
                    int len1 = dp[i].length;
                    int len2 = dp[j].length + 1;
                    if(len2 >= len1) {
                        int[] tmp = new int[len2];
                        System.arraycopy(dp[j],0,tmp,0,len2-1);
                        tmp[len2-1] = arr[i];
                        dp[i] = dicPrimary(dp[i],tmp);
                    }
                }
            }
            if(dp[i].length > max.length) {
                max = dp[i];
                //print(max);
            }else if(dp[i].length == max.length) {
                max = dicPrimary(dp[i], max);
                //print(max);
            }
        }
        return max;
    }

    // 按字典顺序比较
    private static int[] dicPrimary(int[] a, int[] b) {
        for (int i = 0; i < a.length; i++) {
            if (a[i] < b[i]) {
                return a;
            }else if (a[i] > b[i]) {
                return b;
            }
        }
        return a;
    }

    static void print(int[] arr) {
        if (arr == null || arr.length == 0) return;
        for (int i : arr) {
            System.out.print(i + " ");
        }
        System.out.println();
    }
}
